package com.changpeng.shake.handler.exception;


import com.changpeng.shake.common.Result;
import com.changpeng.shake.common.enums.ResultEnum;
import com.changpeng.shake.common.exception.GeneralBusinessException;
import com.changpeng.shake.common.exception.VerificationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice(basePackages = "com.changpeng.shake.controller")
@ResponseBody
public class GlobalExceptionHandler {



    // 校验错误异常
    @ExceptionHandler(VerificationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result<String> handlePasswordErrorException(VerificationException ex) {
        //如通过redis进行密码错误计数器累加操作
        //。。。。。。。。。。。。。
        //日志记录等。。。。。。。。。
        String message = ex.getMessage();
        if (message!=null){
            return Result.error(message);
        }
        return Result.error(ex.getCodeEnum());
    }



    //通用业务异常处理
    @ExceptionHandler(GeneralBusinessException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result<String> generalBusinessExceptionHandler(GeneralBusinessException ex) {
        return Result.error(ex.getMessage());
    }

    /**
     *
     * 通用异常处理(用于处理不可预知的异常)
     */
    @ExceptionHandler(Exception.class)
    public Result<String> exceptionHandler(Exception ex) {
        return Result.error(ResultEnum.FAIL);
    }


}
